<template>
  <div>
    <!-- 商品列表 -->
     <h1>商品列表</h1>
    <div class="shop-list">
        <div class="shop-item">
            <div v-for="(item,index) in arr" :key="index" @click="shopitem(index)">{{item}}</div>
        </div>
        <div class="shops">
            <div v-for="(item,index) in tab" :key="index" class="shop" @click="addCart(item)">
                <img :src="item.img" alt="">
                <span>{{item.name}}</span>
                <span class="price">{{item.price | formatPrice}}</span>
            </div>
        </div>
    </div>
    <!-- 购物车 -->
     <h1>购物车</h1>
     <div class="cart">
         <ul class="cart-list">
             <li v-for="(item,index) in cart" :key="index" class="cart-item">
                 <input type="checkbox" v-model="item.is" @click="item.is=!item.is" :checked="item.is">
                <img :src="item.img">
                <span>{{item.name}}</span>
                <span class="price">{{item.price * item.count | formatPrice}}</span>
                <button class="jian" @click="jian(item)">-</button>
                <input type="text" v-model="item.count" class="count">
                <button class="jia" @click="item.count++">+</button>
                <button @click="delCart(index)">删除</button>
             </li>
         </ul>
         <div class="cart-bottom">
            <input type="checkbox" v-model="all" @click="quan" :checked="all">
            <span>全选</span>
            <span>合计:{{total | formatPrice}}</span>
         </div>
     </div>
  </div>
</template>

<script>
export default {
   data() {
    
    return {
        all:false,
        arr:['生活用品','电器','数码'],
        arrs:[{
            pid:0,
            id:0,
            name:'牙刷',
            price:100,
            img:'https://img.tukuppt.com/png_preview/00/12/44/ZF3K6RR6aD.jpg!/fw/780'
        },{
            pid:0,
            id:1,
            name:'电吹风',
            price:200,
            img:'https://tse3-mm.cn.bing.net/th/id/OIP-C.as3bVErF_0IJTmjlYBEtjAHaHa?w=203&h=203&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3'
        },{
            pid:1,
            id:2,
            name:'手机',
            price:300,
            img:'https://tse1-mm.cn.bing.net/th/id/OIP-C.EnMziFOiJlxfUuZmfbVhAAHaHa?w=202&h=203&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3'
        },{
            pid:1,
            id:3,
            name:'电脑',
            price:400,
            img:'https://tse2-mm.cn.bing.net/th/id/OIP-C.mQYi3jCjgSrdO-1z-WSuQwHaEJ?w=318&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3'
        },{
            pid:2,
            id:4,
            name:'电视',
            price:500,
            img:'https://tse3-mm.cn.bing.net/th/id/OIP-C.1rZjFgYiIClEqFJ8tlfE8QHaHa?w=196&h=196&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3'
        },{
            pid:2,
            id:5,
            name:'路由器',
            price:600,
            img:'https://tse4-mm.cn.bing.net/th/id/OIP-C.vVousOBaA8j3Q9czNZf8fAHaHa?w=172&h=180&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3'
        }],
        tab:[{
            pid:0,
            id:0,
            name:'牙刷',
            price:100,
            img:'https://img.tukuppt.com/png_preview/00/12/44/ZF3K6RR6aD.jpg!/fw/780'
        },{
            pid:0,
            id:1,
            name:'电吹风',
            price:200,
            img:'https://tse3-mm.cn.bing.net/th/id/OIP-C.as3bVErF_0IJTmjlYBEtjAHaHa?w=203&h=203&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3'
        }],
        cart:[]
    }
  },
  methods: {
    shopitem(index) {
        //tab切换
        this.tab = this.arrs.filter(item => item.pid === index);
    },
    addCart(item) {
        this.cart.push({
            id:this.cart.length,
            name:item.name,
            price:item.price,
            img:item.img,
            count:1,
            is:false,
        });
  },
  delCart(index) {
    this.cart.splice(index,1);
  },
  jian(item) {
    if(item.count > 1) {
        item.count--;
    }
  },
  quan() {
    this.all = !this.all;
    if(this.all) {
        this.cart.forEach(item => {
            item.is = true;
        });
    }else{
        this.cart.forEach(item => {
            item.is = false;
        });
    }
  }
},
  computed: {
    total() {
        let total = 0;
            this.cart.forEach(item => {
                if(item.is){
                    total += item.price * item.count;
                }
        });
        return total;
    }
  },
  filters: {
    formatPrice(value) {
        return '￥' + value.toFixed(2);
    }
  }
}
</script>
<style lang='less' scoped>
    .shop-list {
        width: 900px;
        height: 500px;
        margin: auto;
        .shop-item{
            width: 900px;
            height: 50px;
            background-color: #f0f0f0;
            >div{
                display: inline-block;
                width: 300px;
                height: 50px;
                line-height: 50px;
                text-align: center;
                cursor: pointer;
            }
        }
        .shops{
            width: 900px;
            height: 450px;
            >div{
                display: inline-block;
                width: 200px;
                height: 250px;
                margin: 10px;
                position: relative;
                cursor: pointer;
                img{
                    width: 100%;
                    height: 150px;
                }
                span{
                    position: absolute;
                    bottom: 0;
                    left: 0;
                    width: 100%;
                    height: 30px;
                    line-height: 30px;
                    text-align: center;
                    color: #fff;
                    background-color: rgba(0,0,0,0.5);
                }
                .price{
                    position: absolute;
                    bottom: 30px;
                    left: 0;
                    width: 100%;
                    height: 30px;
                    line-height: 30px;
                    text-align: center;
                    color: #fff;
                    background-color: rgba(0,0,0,0.5);
                }
            }
        }
    }
    .cart{
        width: 900px;
        margin: auto;
        .cart-list{
            width: 900px;
            background-color: #f0f0f0;
            >li{
                list-style: none;
                width: 900px;
                height: 100px;
                margin: 10px;
                position: relative;
                border-bottom: 1px solid #ccc;
                >input[type="checkbox"]{
                    position: absolute;
                    top: 10px;   
                    left: 0;
                }
                img{
                    width: 100px;
                    height: 100px;
                    float: left;
                    margin-right: 10px;
                }
                span{
                    float: left;
                    width: 200px;
                    height: 100px;
                    line-height: 100px;
                    text-align: center;
                }
                .price{
                    position: absolute;
                    top: 35px;
                    left: 250px;
                    width: 100px;
                    height: 30px;
                    line-height: 30px;
                    text-align: center;
                    color: #fff;
                    background-color: rgba(0,0,0,0.5);
                }
                .count{
                    position: absolute;
                    top: 35px;
                    left: 500px;
                    width: 100px;
                    height: 30px;
                    line-height: 30px;
                    text-align: center;
                    color: #fff;
                    background-color: rgba(0,0,0,0.5);
                }
                button.jian{
                    position: absolute;
                    top: 35px;
                    left: 470px;
                    width: 30px;
                    height: 35px;
                    line-height: 30px;
                    text-align: center;
                    color: #fff;
                    background-color: rgba(0,0,0,0.5);
                    border: none;
                    cursor: pointer;
                }
                button.jia{
                    position: absolute;
                    top: 35px;
                    left: 605px;
                    width: 30px;
                    height: 35px;
                    line-height: 30px;
                    text-align: center;
                    color: #fff;
                    background-color: rgba(0,0,0,0.5);
                    border: none;
                    cursor: pointer;
                }
                button{
                    position: absolute;
                    top: 10px;
                    right: 10px;
                    width: 50px;
                    height: 30px;
                    line-height: 30px;
                    text-align: center;
                    color: #fff;
                    background-color: rgba(0,0,0,0.5);
                    border: none;
                    cursor: pointer;
                }
                button:hover{
                    background-color: #f00;
                }
            }}
            .cart-bottom{
                width: 950px;
                height: 50px;
                background-color: #f0f0f0;
                >input[type="checkbox"]{
                   margin-left: 60px;
                }
                >span{
                    margin-left: 100px;
                    font-size: 20px;
                    font-weight: bold;
                    color: #f00;
                }
                >span:nth-child(2){
                    margin-left: 10px;
                    font-size: 20px;
                    font-weight: bold;
                    color: #f00;
                }
            }   
    }
</style>